import org.apache.commons.codec.net.URLCodec
import org.apache.commons.codec.EncoderException
import org.apache.commons.lang.StringUtils
import java.text.SimpleDateFormat
import org.hibernate.SessionFactory

class House extends RealtyOfferObject {
    House() {
        super.realtyTypeId = Constants.OBJECT_TYPE_HOUSE_ID;
        super.realtyType = Constants.OBJECT_TYPE_HOUSE;
    }

    public Double fillDetails(String[] details, Date date) {
        String extIdStr = details[0];
        String roomsStr = details[1];
        String priceStr = details[2];
        String addressStr = details[3] + " / " + details[4];
        String levelStr = details[5];
        String partStr = details[6];
        String commonSquareStr = details[7];
        String zoneSquareStr = details[8];
        String phoneExistStr = details[9];
        String gasExistStr = details[10];
        String waterExistStr = details[11];
        String sewerageExistStr = details[12];
        String advInfoStr = details[13];
        String telNumbersStr = details[14];

        String encodedAddress = addressStr;
        String encodedAdvInfo = advInfoStr;
        URLCodec urlCodec = new URLCodec();
        try {
            encodedAddress = urlCodec.encode(encodedAddress);
            encodedAdvInfo = urlCodec.encode(encodedAdvInfo);
        } catch (EncoderException e) {
        }

        Long extId = 0L;
        Long rooms = 0L;
        Double price = 0L;
        Long level = 0L;
        //Long part = 0L;
        Double commonSquare = 0L;
        Double zoneSquare = 0L;

        if (StringUtils.isNotEmpty(extIdStr) && StringUtils.isNumeric(extIdStr))
            try {extId = Long.parseLong(extIdStr);} catch (Exception e) {};
        if (StringUtils.isNotEmpty(roomsStr) && StringUtils.isNumeric(roomsStr))
            try {rooms = Long.parseLong(roomsStr);} catch (Exception e) {};
        if (StringUtils.isNotEmpty(levelStr) && StringUtils.isNumeric(levelStr))
            try {level = Long.parseLong(levelStr);} catch (Exception e) {};
//        if (StringUtils.isNotEmpty(partStr) && StringUtils.isNumeric(partStr))
//            try {part = Long.parseLong(partStr)} catch (Exception e) {};

        if (StringUtils.isNotEmpty(commonSquareStr) && StringUtils.isNumeric(commonSquareStr))
            try {commonSquare = Double.parseDouble(commonSquareStr)} catch (Exception e) {};
        if (StringUtils.isNotEmpty(zoneSquareStr) && StringUtils.isNumeric(zoneSquareStr))
            try {zoneSquare = Double.parseDouble(zoneSquareStr)} catch (Exception e) {};

        if (StringUtils.isNotEmpty(priceStr) && StringUtils.isNumeric(priceStr))
            try {price = Double.parseDouble(priceStr);} catch (Exception e) {};

        externalIdList.add(extIdStr);
        realtyObject = new RealtyObject(externalId: extId);
        priceObject = new Price(priceValue: price, date: date);
        realtyObject.addToPrice(priceObject);
        houseDetails = new HouseDetails(
                address: encodedAddress,
                rooms: rooms,
                telNumbers: telNumbersStr,
                level: level,
                part: partStr,
                zoneSquare: zoneSquare,
                houseSquare: commonSquare,
                gas: gasExistStr,
                water: waterExistStr,
                sewerage: sewerageExistStr,
                phone:phoneExistStr,
                advInfo: encodedAdvInfo);
        realtyObject.houseDetails = houseDetails;
        flatDetails = null;
        //realtyObject.houseDetails = null;
        //println("!#="+ ++offerCounter +"!!"+ extIdStr+"--"+roomsStr+"--"+priceStr+"--"+addressStr+""+levelStr+""+levelMaxStr);
        return price;
    };

    public List<RealtyObject> findRealtyByExternalId(Long externalId) {
        return RealtyObject.findAll("from RealtyObject ro where ro.externalId = ? and ro.houseDetails is not null", [externalId]);
    };
    public List<RealtyObject> findExcludedRealty (String csvStr){
        return RealtyObject.findAll("from RealtyObject ro where ro.externalId not in (" + csvStr + ") and ro.houseDetails is not null ");
    };
    
}